<?php
namespace app\home\controller\user;

use app\BaseController;


use app\common\model\UserRealName as UserRealNameModel;
use app\common\model\User as models;
use app\common\traites\PublicCrudTrait;

use think\exception\ValidateException;
use think\facade\Db;
use hg\apidoc\annotation as Apidoc;

/**
 *
 * @Apidoc\Title("个人中心")
 * */
class RealName extends BaseController
{
    protected $model;
    use PublicCrudTrait;

    public function initialize()
    {
        $this->model = new models();
        parent::initialize(); // TODO: Change the autogenerated stub
    }


    /**
     * @Apidoc\Title("获取 实名信息")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回数据")
     */
    public function get_realName(){
        $uid = session('home_user.id');
        $res=(new UserRealNameModel())->where(['user_id'=>$uid])->find();
        return show($res);
    }

    /**
     * @Apidoc\Title("添加 实名认证 接口自动认证版本")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("card_id", type="varchar",require=true, desc="身份证号")
     * @Apidoc\Param("true_name", type="varchar",require=true, desc="真实姓名")
     * @Apidoc\Param("positive_url", type="varchar",require=true, desc="身份证正面照片")
     * @Apidoc\Param("back_url", type="varchar",require=true, desc="身份证反面照片")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function add_realName_jiekou(){
        $user_id= session('home_user.id');
        //过滤数据
        $postField = 'card_id,true_name';
        $params   = $this->request->only(explode(',', $postField), 'post', null);


        try {
            $this->validate($params,[
                'card_id' => 'require|max:100', //
                'true_name' => 'max:50', //
                // 'positive_url' => 'max:200', //
                // 'back_url' => 'max:50', //
            ],
                [
                    'card_id' => 'card_id is required',
                    'true_name' => 'true_name is required',
                    // 'positive_url' => 'positive_url is required',
                    // 'back_url' => 'back_url is required',
                ]
            );
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return show([],config('ToConfig.http_code.error'),$e->getError());
        }

        $UserRealNameModel= new UserRealNameModel();
        $res=$UserRealNameModel->where('card_id',$params['card_id'])->find();
        if ($res){
            return show([],config('ToConfig.http_code.error'),'身份证号已经被占用');
        }
        $user_name = session('home_user.user_name');
        $count=$UserRealNameModel->where('user_id',$user_id)->value('id');

        if ($count){
            //删除原来的 覆盖上
            $UserRealNameModel->where('user_id',$user_id)->delete();
        }
        $params['user_id']=$user_id;
        $params['status']=0;
        $params['user_name']=$user_name;
        $res=$UserRealNameModel->insert($params);
        // 调用 认证接口
        $check_res = (new RealName())->check_real_name($params['true_name'],$params['card_id']);

        if ($check_res == 1) {
            // 实名成功
            $money_realname_give = floatval(300000.00) ;
            // 更新实名申请状态
            $user = $this->model->where(['id' => $user_id])->find();
            // 更新用户 捐赠金
            $user->save([
                'is_real_name' => 1, // 更新下线状态
                'money_juanzeng' => $user->money_juanzeng + $money_realname_give, //实名认证添加 公益金
            ]);
            MoneyLog::flowing(1, 1, 1, 301, $user, $money_realname_give, 0, '实名认证获得公益金', 'money_juanzeng');
            
            // 上级增加一次抽奖机会
            if(isset($user->agent_id_1)){
                $draw_times = 1;
                $agent_id = $user->agent_id_1;
                $times = [
                    'user_id' => $agent_id,
                    'send_times' => $draw_times,
                    'create_time' => date('Y-m-d H:i:s'),
                    'mark' => '下线实名赠送抽奖次数',
                ];
                TouziLotteryTimes::create($times);
                //增加抽奖次数
                User::increaseBy('choujiang_times', $agent_id, $draw_times);
            }
                        
            return show([], 1, 'succeeded');
        } else {
            return show([], config('ToConfig.http_code.error'), '认证失败');
        }

    }
    /**
     * @Apidoc\Title("添加 实名认证 后台控制版本")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("card_id", type="varchar",require=true, desc="身份证号")
     * @Apidoc\Param("true_name", type="varchar",require=true, desc="真实姓名")
     * @Apidoc\Param("positive_url", type="varchar",require=true, desc="身份证正面照片")
     * @Apidoc\Param("back_url", type="varchar",require=true, desc="身份证反面照片")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function add_realName_houtaikongzhi(){
        $user_id= session('home_user.id');
        //过滤数据
        $postField = 'card_id,true_name,positive_url,back_url';
        $params   = $this->request->only(explode(',', $postField), 'post', null);


        try {
            $this->validate($params,[
                'card_id' => 'require|max:100', //
                'true_name' => 'max:50', //
                'positive_url' => 'max:200', //
                'back_url' => 'max:50', //
            ],
                [
                    'card_id' => 'card_id is required',
                    'true_name' => 'true_name is required',
                    'positive_url' => 'positive_url is required',
                    'back_url' => 'back_url is required',
                ]
            );
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return show([],config('ToConfig.http_code.error'),$e->getError());
        }

        $UserRealNameModel= new UserRealNameModel();
        $res=$UserRealNameModel->where('user_id',$user_id)->find();
        if ($res){
            return show([],config('ToConfig.http_code.error'),'card number already exists');
        }
        $user_name = session('home_user.user_name');
        $count=$UserRealNameModel->where('user_id',$user_id)->value('id');

        if ($count){
            //删除原来的 覆盖上
            $UserRealNameModel->where('user_id',$user_id)->delete();
        }
        $params['user_id']=$user_id;
        $params['status']=0;
        $params['user_name']=$user_name;
        $res=$UserRealNameModel->insert($params);

        // 更新实名申请状态
        $this->model->where(['id'=>$user_id])->update(['is_real_name'=>2]); // 更新下线状态

        if ($res){
            return show([],1,'succeeded');
        }else{
            return show([],config('ToConfig.http_code.error'),'failed. Please enter the correct login password');
        }

    }

    /**
     * @Apidoc\Title("编辑 实名信息")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="ID序号")
     * @Apidoc\Param("card_id", type="varchar",require=true, desc="身份证号")
     * @Apidoc\Param("true_name", type="varchar",require=true, desc="真实姓名")
     * @Apidoc\Param("positive_url", type="varchar",require=true, desc="身份证正面照片")
     * @Apidoc\Param("back_url", type="varchar",require=true, desc="身份证反面照片")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function edit_realName(){
        $user_id= session('home_user.id');
        //过滤数据
        $postField = 'id,card_id,true_name,positive_url,back_url';
        $params   = $this->request->only(explode(',', $postField), 'post', null);

        try {
            $this->validate($params,[
                'id' => 'require|number', //
                'card_id' => 'require|max:100', //
                'true_name' => 'max:50', //
                'positive_url' => 'max:400', //
                'back_url' => 'max:400', //
            ],
                [
                    'id' => 'ID must be passed',
                    'card_id' => 'card_id is required',
                    'true_name' => 'true_name is required',
                    'positive_url' => 'positive_url is required',
                    'back_url' => 'back_url is required',
                ]
            );
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return show([],config('ToConfig.http_code.error'),$e->getError());
        }

        $UserRealNameModel= new UserRealNameModel();
        $find=$UserRealNameModel->where('id',$params['id'])->find();

        if (!$find){
            return show([],config('ToConfig.http_code.error'),'Data does not exist');
        }
        $res=$UserRealNameModel->update($params);
        if ($res){
            return show([]);
        }else{
            return show([],config('ToConfig.http_code.error'),'failed');
        }
    }

// 类结束了
}
